<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GSession
 *
 * @author Luis Alberto
 */
include("GUsuario.php");
include("GModulo.php");
include './libs/adodb/adodb.inc.php';
include("./configuracion.php");
class GSession
{
    private $db = null;
    private $gusuario = null;
    private $gmodulo = null;
    public function __construct()
    {
        $this->gusuario = new GUsuario();
        $this->gmodulo = new GModulo();
    }
    public function iniciar_session($emailusername, $password)
    {
        $password = md5($password);
        $db = ADONewConnection(DRIVER);
 	$db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        // Probando Ejemplo SQL IYECTION
        //$cns = "select * from cliente where email = 'ciromarcelo@gmail.com' -- ";//' or username = '$emailusername' and password = '$password'";        
        $cns = "select * from usuario where usr_email = '$emailusername' or usr_username = '$emailusername' and usr_password = '$password'";
        //echo "$cns";
        $rs = $db->Execute($cns);
        if ($rs->RecordCount() == 1)
        {
            $dato = $rs->fetchRow();
            $_SESSION['log_ini'] = true;
            $_SESSION['usr_cod'] = $dato['usr_cod'];
            return true;
        }
        else
        {
            return false;
        }
    }
    public function inicio_session()
    {
        return $_SESSION['log_ini'];
    }
    public function cerrar_sesion()
    {
        $_SESSION['log_ini'] = FALSE;
        session_destroy();
    }
    public function nombre_completo($usr_cod)
    {
        $usr = new Usuario();
        $usr = $this->gusuario->Obtener($usr_cod);
        $completo = $usr->getusr_nombre();
        $completo .=$usr->getusr_apellidos();
        return $completo;
    }
    public function get_usuario($usr_cod)
    {
        $usr = new Usuario();
        $usr = $this->gusuario->Obtener($usr_cod);
        return $usr;
    }
    public function get_menu($usr_percod)
    {
        $db = ADONewConnection(DRIVER);
 	$db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from modulo where mod_cod in (select mod_cod from privilegio pri  where pri.per_cod = $usr_percod)";
        $rs = $db->Execute($cns);
        if($rs)
        {
            $menu = "<ul id=\"topnav\">";
            $menu .="<li><a href=\"index.php\"> Home </a></li>";
            while($row = $rs->FetchRow())
            {
                $query =" select * from menu where mod_cod = '".$row['mod_cod']."'";
                $rs2 = $db->Execute($query);
                $n = $rs2->RecordCount();
                if($n == 0)
                {                    
                  $menu .= "<li><a href=\"".utf8_encode($row['mod_urlcorta'])."\">".utf8_encode($row['mod_nombre'])."</a></li>";
                }else
                {
                  $menu .= "<li><a href=\"".utf8_encode($row['mod_urlcorta'])."\">".utf8_encode($row['mod_nombre'])."</a><span>";
                  //$menu .= "<li><a class=\"MenuBarItemSubmenu\" href=\"#\">".$row_parent['menu']."</a><ul>";
                  while($row_smenu = $rs2->FetchRow())
                  {
                    $menu .="<a href=\"".utf8_encode($row['mod_urlcorta'])."?submod=".utf8_encode($row_smenu['men_cod']);
                    $menu .="\" title=\"".utf8_encode($row_smenu['men_descripcion']);
                    $menu .="\" >".utf8_encode($row_smenu['men_nombre'])."</a> |";
                  }
                  $menu.="</span></li>";

                }
            }
            $menu .="</ul>";
            return $menu;
        }
        return null;
    }
    public function get_sub_modulo($submod)
    {

        $info = $this->gmodulo->getSubmodulo($submod);
        return $info;
         /*
        $db = ADONewConnection(DRIVER);
 	$db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from menu where men_cod = $submod";
        echo $cns;
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
	*/
    }
    public function tieneAcceso($codper, $idmod)
    {
        $db = ADONewConnection(DRIVER);
 	$db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from privilegio where per_cod = $codper and mod_cod = $idmod";
        $rs = $db->Execute($cns);
        if ($rs->RecordCount() == 1)
            return true;
        else
            return false;
    }
    public function Abecedario()
    {
        $lista = array();
        foreach(range('A', 'Z') as $letra)
        {
          $lista[] = $letra;
        }
        return $lista;
    }
    public function Ultimosdies($usrcod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from mensaje where usr_cod =$usrcod order by codigo desc limit 4";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function getConfSocial($cscod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select * from configuracionsocial where cs_cod = $cscod";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
        }
        return $dato;
    }
}
?>
